package com.andrew.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.andrew.dao.CustomerDao;
import com.andrew.dao.impl.BookDaoImpl;
import com.andrew.dao.impl.CustomerDaoImpl;
import com.andrew.entity.Customer;
import com.andrew.service.CustomerService;
import com.andrew.util.JDBCUtils;

public class CustomerServiceImpl implements CustomerService{
	
	private CustomerDao customerDao=new CustomerDaoImpl();
	@Override
	public Customer login(String phone, String password) throws SQLException {
		Customer login = customerDao.findCustomer(phone, password);
		return login;
	}

	/***
	 * 注册：
	 *		param：用户对象
	 *		return: "success"注册成功
	 *				"fail"注册失败
	 *		
	 */
	@Override
	public String register(Customer customer)throws SQLException {
		int customerNumber=4;
		int cardNumber=4;
		Connection conn=JDBCUtils.getConn();
		try {
		conn.setAutoCommit(false);
		customerNumber=customerDao.InsertCustomer(conn, customer);
		cardNumber=customerDao.InsertCart(conn, customer);
		conn.commit();
		return "success";
		}catch(Exception e){
			conn.rollback();
			System.out.println(cardNumber+","+customerNumber);
			return "fail";
		}finally {
			conn.setAutoCommit(true);
			conn.close();
			System.out.println("enter finally");
		}
	}

	@Override
	public List<Customer> getAllCustomer() throws SQLException {
		return customerDao.findAllCustomer();
	}

	@Override
	public List<Customer> getAllCommonCustomer() throws SQLException {
		return customerDao.findAllCommonCustomer();
	}

}
